var API = require('API').survey.qa;
var GRID = require('question.aggrid');

module.exports = ['$scope', '$rootScope', '$location', '$routeParams', '$timeout',
    function ($scope, $rootScope, $location, $routeParams, $timeout) {

        $scope.add_open = false;
        $scope.edit_open = false;
        $scope.grid = GRID.grid;

        $scope.record_add = {};
        $scope.record_edit = {};

        $scope.answers_add = {
            data: [],
            current_sn: "A"
        };

        $scope.submit_add = function () {
            //
            API.add_question($scope.record_add, function (json) {
                //添加答案
                var answers=$scope.answers_add.data;
                var total=answers.length;
                $.each(answers,function(i,v){
                    API.add_answer(json.id,v, function () {
                        if(i+1==total){
                            $scope.$apply(function () {
                                $scope.refresh();
                            });
                        }
                    }, function (e) {
                        alert(e)
                    });

                });

            }, function (e) {
                alert(e);
            });
        };

        $scope.open_edit = function (data) {
            $scope.edit_open = true;
            $scope.record_edit = data;
        };

        GRID.on('open', function (row) {
            $scope.$apply(function () {
                $scope.open_edit(row);
            });
        });

        GRID.on('del', function (row) {
            //
            API.del(row.id, function () {
                $scope.$apply(function () {
                    $scope.refresh()
                });
            }, function (e) {
                alert(e);
            });
        });

        $scope.submit_edit = function () {
            //
            API.upd($scope.record_edit, function () {
                $scope.$apply(function () {
                    $scope.refresh();
                });
            }, function (e) {
                alert(e);
            });
        };

        $scope.refresh = function () {
            GRID.refresh();
            $scope.add_open = false;
            $scope.edit_open = false;

            $scope.record_add = {};
            $scope.record_edit = {};
        };


        $scope.show_answer = function () {
            var name = prompt("请输入答案选项", "答案");
            if (name != null && name != "") {

                var sn = $scope.answers_add.current_sn;
                if (sn == 'A') $scope.answers_add.current_sn = 'B';
                if (sn == 'B') $scope.answers_add.current_sn = 'C';
                if (sn == 'C') $scope.answers_add.current_sn = 'D';
                if (sn == 'D') $scope.answers_add.current_sn = 'E';
                if (sn == 'E') $scope.answers_add.current_sn = 'F';

                $scope.answers_add.data.push({
                    content: name,
                    sn: sn
                });
            }
        }


    }];